package com.wxl.asmgradleplugin.myplugin

import com.android.build.api.artifact.ScopedArtifact
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.ScopedArtifacts
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.register
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.File

class MyPlugin3 : Plugin<Project> {
    override fun apply(project: Project) {
        println("---------------MyPlugin3 333 ------------------")
        println("---------------MyPlugin3 333 ${project.layout.buildDirectory.asFile.get().absolutePath}------------------")
//        var kotlinClassDir = ""
//        project.tasks.withType(KotlinCompile::class.java) {
//            doLast {
//                kotlinClassDir = destinationDirectory.asFile.getOrNull().toString()
//                println("---------------MyPlugin3 333 kotlinClassDir=$kotlinClassDir")
//            }
//        }
//        var javaClassDir = ""
//        project.tasks.withType(JavaCompile::class.java) {
//            doLast {
//                javaClassDir = destinationDirectory.asFile.getOrNull().toString()
//                println("---------------MyPlugin3 333 javaClassDir=$javaClassDir")
//            }
//        }
        project.plugins.withType(AppPlugin::class.java) {
            val android = project.extensions.getByType(BaseExtension::class.java)
            val androidComponents =
                project.extensions.getByType(AndroidComponentsExtension::class.java)
            androidComponents.onVariants { variant ->
                println("---------------333 variant=${variant.name}")
//                var javaClassDir = ""
//                var kotlinClassDir = ""
//                project.afterEvaluate {
//                    val javaCompile = project.tasks.named(
//                        "compile${variant.name.capitalize()}JavaWithJavac",
//                        JavaCompile::class.java
//                    ).get()
//                    javaClassDir = javaCompile.destinationDirectory.asFile.getOrNull().toString()
//                    println("--------------- 333 javaClassDir=$javaClassDir")
//
//                    val kotlinCompile = project.tasks.named(
//                        "compile${variant.name.capitalize()}Kotlin",
//                        KotlinCompile::class.java
//                    ).get()
//                    kotlinClassDir =
//                        kotlinCompile.destinationDirectory.asFile.getOrNull().toString()
//                    println("--------------- 333 kotlinClassDir=$kotlinClassDir")
//                }
//
//                println("---------------MyPlugin3 333 javaClassDir=${javac.destinationDirectory.asFile.get()}")

//                val transformTask = project.tasks.register(
//                    "${variant.name}MyTransformTask",
//                    MyTransformTask::class.java
//                )
                val transformTask =
                    project.tasks.register<MyTransformTask>("${variant.name}MyTransformTask") {
                        println("------ 333 register------")
//                         set android sdk path.
                        androidSdkDir.set(AndroidJarProvider.getAndroidJar(android).absolutePath)
//                         set compile classpath. The returned [FileCollection] should not be resolved until execution time.
                        compileClasspath.set(variant.compileClasspath)
//                        mergedManifest.set(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST))
//                        producedManifest.set(produceManifestTask.flatMap { it.outputManifest })
                        // Java 输出目录
//                        inputDir.set(File(javaClassDir))
//                        // Kotlin 输出目录
//                        inputDir2.set(File(kotlinClassDir))
                    }
                variant.artifacts.forScope(ScopedArtifacts.Scope.ALL)
                    .use(transformTask)
                    .toTransform(
                        ScopedArtifact.CLASSES,
                        MyTransformTask::allJars,
                        MyTransformTask::allDirs,
                        MyTransformTask::output
                    )
            }
        }

    }
}

interface AndroidJarProvider {
    fun getAndroidJar(android: BaseExtension): File

    companion object DEFAULT : AndroidJarProvider {
        override fun getAndroidJar(android: BaseExtension): File {
            val sdkPath = arrayOf(
                android.sdkDirectory.absolutePath,
                "platforms",
                android.compileSdkVersion
            ).joinToString(File.separator)
            return File(sdkPath, "android.jar")
        }
    }
}